﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;

namespace Docx2PDF
{
    public partial class mainForm : Form
    {
        public mainForm()
        {
            InitializeComponent();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to exit?","Exit",MessageBoxButtons.YesNo)==DialogResult.Yes)
                this.Close();
        }

        private void btnFile_Click(object sender, EventArgs e)
        {
            targetFileDialog.ShowDialog();
            txtFilename.Text = targetFileDialog.FileName;
        }

        private void btnFolder_Click(object sender, EventArgs e)
        {
            targetFolderDialog.ShowDialog();
            txtDestFolder.Text = targetFolderDialog.SelectedPath;
        }

        private void btnSign_Click(object sender, EventArgs e)
        {
            Microsoft.Office.Interop.Word.ApplicationClass app = new Microsoft.Office.Interop.Word.ApplicationClass();

            try
            {
                // Open document
                app.Documents.Open(txtFilename.Text);
                app.Visible = false;
                app.WindowState = Microsoft.Office.Interop.Word.WdWindowState.wdWindowStateMinimize;
                //Document objDoc = app.ActiveDocument;

                object fileName = txtDestFolder + @"\signedDocument.pdf";
                object fileFormat = WdSaveFormat.wdFormatPDF;
                object missing = System.Reflection.Missing.Value;

                app.ActiveDocument.SaveAs(ref fileName, ref fileFormat,
                    ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref missing, ref missing);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (app != null)
                {
                    app.Quit(Type.Missing,Type.Missing,Type.Missing);
                }
            }
        }
    }
}
